//import com.analyst;
import com.analyst.AnalysisApplication;
import com.analyst.entity.Job;
import com.analyst.service.IDependService;
import com.analyst.service.ILogService;
import com.analyst.service.impl.TimeService;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.util.*;

@SpringBootTest(classes = {AnalysisApplication.class})
public class test {
    @Resource
    private ILogService logService;
    @Resource
    private IDependService dependService;
    @Resource
    private TimeService timeService;
    @Test
    public void test() {
        List<Job> jobs =
                dependService.getJobList();
        System.out.println(jobs);
    }
    @Test
    public void testTime()throws Exception{
//        List<SqlJob> sqlJobs = TimeService.initJobs();
        List<Job> sqlJobs = null;
        Job job1 = sqlJobs.get(0);
        Job job4=timeService.getEndJob(job1);

        //记录更新前时间

        long oldTime = job4.getBeginTime().getTime();
        List<Job> keyRoute1 = timeService.getKeyRoute(job1);
        System.out.println(keyRoute1);
        long begin = System.currentTimeMillis();
//          串行执行
//         timeService.serialUpdate(job1, 5 * 1000);
        //并行执行
        timeService.parallelUpdateV1(job1, 5 * 1000);
        long end = System.currentTimeMillis();

        System.out.println();
        List<Job> keyRoute2= timeService.getKeyRoute(job1);

        System.out.println(keyRoute2);

        long newTime = job4.getBeginTime().getTime();
        long improveTime = oldTime - newTime;
        System.out.println("末节点提升时间：："+ improveTime);
        System.out.println("耗时：(/ms)"+(end-begin));
    }

    @Test
    public void testTime1()throws Exception{
        //自拟数据
//        List<SqlJob> sqlJobs = TimeService.initJobs();
        //从etl和dependency获取数据
        List<Job> sqlJobs = null;

        Scanner scanner = new Scanner(System.in);
     

        Job updateJob=null;
        while (updateJob==null){
            System.out.println("请输入更新节点id（system+job+txdate）：");
            String jobId = scanner.next();
            for (Job sqlJob : sqlJobs) {
                if (sqlJob.getId().equals(jobId)){
                    updateJob=sqlJob;
                }
            }
            
        }
        
        
        
        List<Job> keyRoute1 = timeService.getKeyRoute(sqlJobs.get(0));
        System.out.println("更新前的关键路径");
        for (Job job : keyRoute1) {
            System.out.println(job);
        }
        Job endJob = timeService.getEndJob(sqlJobs.get(0));
        //记录更新前时间
        long endTime = endJob.getEndTime().getTime();

        System.out.println("输入提升秒数 /s");
        long improveTime=scanner.nextLong();

        Job queryJob=null;
        String queryJobId = "";
        while (queryJob==null){
            System.out.println("请输入查询的节点id（system+job+txdate）：");
            queryJobId= scanner.next();
            for (Job sqlJob : sqlJobs) {
                if (sqlJob.getId().equals(queryJobId)){
                    queryJob=sqlJob;
                }
            }
        }
        long queryOldEndTime = queryJob.getEndTime().getTime();

        //计时
        long begin = System.currentTimeMillis();

        
        //sqlJobs.get(0) 需要更新的节点
        //60000  提升时间
        
//        timeService.parallelUpdate(updateJob,improveTime*1000);

        long end = System.currentTimeMillis();
        long queryNewEndTime = queryJob.getEndTime().getTime();

        List<Job> keyRoute = timeService.getKeyRoute(sqlJobs.get(0));
        System.out.println("更新后的关键路径");
        for (Job job : keyRoute) {
            System.out.println(job);
        }



        endJob = timeService.getEndJob(sqlJobs.get(0));
        long newEndTime = endJob.getEndTime().getTime();
        System.out.println("endJob提升了"+ (endTime-newEndTime)/1000+"/s");
        System.out.println( queryJobId+" 提升了"+ (queryOldEndTime-queryNewEndTime)/1000+"/s");
        System.out.println("耗时：(/ms)"+(end-begin));

        
    }

    public int longestConsecutive(int[] nums) {

        HashSet<Integer> set = new HashSet<>();
        for (int num : nums) {
            set.add(num);
        }
        int res=0;
        for (int num : set) {
            if (!set.contains(num-1)){
                int curNum=num+1;
                int count=1;
                while (set.contains(curNum) ){
                    count++;
                    curNum++;
                }
                res=Math.max(res,count);

            }


        }
        return res;

    }



    public int maxArea(int[] height) {
        int i =0;
        int j=height.length-1;
        //i / j 动矮的一边
        int res=0;

        while (i<j){
            res=Math.max(Math.min(height[i],height[j])*(j-i) ,res);
            if (height[i]<height[j]){
                i++;
            }else if (height[i]==height[j]){
                i++;
                j--;

            }else {
                j--;
            }
        }
        return res;


    }
    public int trap(int[] height) {
        int n = height.length;
        int maxLeft=0;
        int maxRight=0;
        int i = 1;
        int j = n-2;
        int min=0;
        int sum=0;
        for (int k = 1; k <height.length-1; k++) {
            if (height[i-1]<height[j+1]){
                //left

                maxLeft=Math.max(maxLeft,height[i-1]);
                min=maxLeft;
                sum=height[i]<min?sum+min-height[i]:sum;
                i++;
            }
            else    {

                maxRight=Math.max(maxRight,height[j+1]);
                min=maxRight;
                sum=height[i]<min?sum+min-height[i]:sum;
                j--;
            }
        }

        return sum;


    }

    public static void main(String[] args) {
        List<Integer> anagrams = findAnagrams("cbaebabacd", "abc");

        System.out.println(anagrams);

    }
    public static List<Integer> findAnagrams(String s, String p) {

        ArrayList<Integer> res = new ArrayList<>();
        if (s.length()<p.length())return res;
        int m = p.length();
        StringBuilder str = new StringBuilder();
        int[] ints = new int[26];
        for (int i = 0; i <m; i++) {
            ints[p.charAt(i)-'a']++;
        }
        for (int i = 0; i < 26; i++) {
            if (ints[i]!=0){
                str.append( (char) (i+'a')).append(ints[i]);
            }
        }
        String key = str.toString();


        int n = s.length();
        int i = 0;
        int j = 0;
        ints = new int[26];
        for ( j = 0; j < m ; j++) {
            ints[s.charAt(j)-'a']++;
        }
        str=new StringBuilder();
        for (int k = 0; k < 26; k++) {
            if (ints[k]!=0){
                str.append((char) (k+'a')).append(ints[k]);
            }
        }
        if (key.equals(str.toString())){
            res.add(i);
        }
        i++;


        while (j<n){
            ints[s.charAt(i-1)-'a']--;
            ints[s.charAt(j)-'a']++;
            str=new StringBuilder();
            for (int k = 0; k < 26; k++) {
                if (ints[k]!=0){
                    str.append( (char) (k+'a')).append(ints[k]);
                }
            }
            if (key.equals(str.toString())){
                res.add(i);
            }
            i++;
            j++;

        }
        return res;
    }
    public int maxProfit(int[] prices) {

        int minLeft=prices[0];
        int maxProfit=0;
        for (int i = 0; i < prices.length; i++) {
            maxProfit=Math.max(maxProfit,prices[i]-minLeft);
            minLeft=Math.min(minLeft,prices[i]);
        }
        return maxProfit;
    }
    public List<List<Integer>> generate(int numRows) {
        ArrayList<List<Integer>> res = new ArrayList<>();

        for (int i = 0; i < numRows; i++) {
            ArrayList<Integer> list = new ArrayList<>();
            for (int j = 0; j <= i; j++) {
                if (j==0||j==i){
                    list.add(1);
                }else {
                    list.add(res.get(i-1).get(j-1)+res.get(i-1).get(j));
                }
            }
            res.add(list);


        }
        return res;
    }

}
